{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "train_size= 60000\n",
      "(300000, 784)\n",
      "(300000,)\n",
      "\n",
      "ps= [0.96340257 0.95450716 0.98216056 0.96442688 0.9762151  0.96528555\n",
      " 0.98130841 0.96108949 0.98809524 0.95626243] 0.9692753386570571\n"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import fetch_mldata\n",
    "import numpy as np\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "\n",
    "mnist = fetch_mldata('mnist-original', data_home='./')\n",
    "X, y = mnist[\"data\"], mnist[\"target\"]\n",
    "#print(X.shape)\n",
    "X_train = X[:60000]\n",
    "y_train = y[:60000]\n",
    "X_test = X[60000:]\n",
    "y_test = y[60000:]\n",
    "shuffle_index = np.random.permutation(60000)\n",
    "X_train, y_train = X_train[shuffle_index], y_train[shuffle_index]\n",
    "X_train, y_train = X_train[:60000], y_train[:60000]\n",
    "\n",
    "train_array = []\n",
    "train_size = X_train.shape[0]\n",
    "for i in range(train_size):\n",
    "#for i in range(122):\n",
    "    train_array.append(X_train[i].reshape(28, 28))\n",
    "    \n",
    "print('train_size=', train_size)\n",
    "\n",
    "def data_en(data_s, direc='u'):\n",
    "    size=len(data_s)\n",
    "    en_ret = np.zeros((size, 784))\n",
    "    if direc == 'u':        \n",
    "        for i in range(size):\n",
    "            trans_data = np.append(data_s[i][1:,:], data_s[i][0:1,:],axis=0)\n",
    "            #print(trans_data.shape)\n",
    "            en_ret[i] = trans_data.reshape(1, -1)\n",
    "    elif direc == 'd':\n",
    "        for i in range(size):\n",
    "            trans_data = np.append(data_s[i][-1:,:], data_s[i][:-1,:],axis=0)\n",
    "            #print(trans_data.shape)\n",
    "            en_ret[i] = trans_data.reshape(1, -1)\n",
    "    elif direc == 'l':\n",
    "        for i in range(size):\n",
    "            trans_data = np.append(data_s[i][:,1:], data_s[i][:,0:1],axis=1)\n",
    "            #print(trans_data.shape)\n",
    "            en_ret[i] = trans_data.reshape(1, -1)\n",
    "    elif direc == 'r':\n",
    "        for i in range(size):\n",
    "            trans_data = np.append(data_s[i][:,-1:], data_s[i][:,:-1],axis=1)\n",
    "            #plt.imshow(trans_data, cmap = matplotlib.cm.binary,interpolation=\"nearest\")\n",
    "            en_ret[i] = trans_data.reshape(1, -1)\n",
    "    return en_ret\n",
    "\n",
    "X_trainu = data_en(train_array, 'u')\n",
    "X_traind = data_en(train_array, 'd')\n",
    "X_trainl = data_en(train_array, 'l')\n",
    "X_trainr = data_en(train_array, 'r')\n",
    "\n",
    "#X_trainA = np.append(X_train, X_trainu, axis=0)\n",
    "X_trainA = np.concatenate((X_train, X_trainu, X_traind, X_trainl, X_trainr), axis=0)\n",
    "y_trainA = np.concatenate((y_train, y_train, y_train, y_train, y_train), axis=0)\n",
    "print(X_trainA.shape)\n",
    "print(y_trainA.shape)\n",
    "\n",
    "knn_clf = KNeighborsClassifier()\n",
    "knn_clf.fit(X_train, y_train)\n",
    "y_pred = knn_clf.predict(X_test)\n",
    "from sklearn.metrics import precision_score, recall_score,confusion_matrix\n",
    "ps = precision_score(y_test, y_pred, average=None)\n",
    "print('\\nps=', ps, np.average(ps))\n",
    "\n",
    "knn_clfA = KNeighborsClassifier()\n",
    "knn_clfA.fit(X_trainA, y_trainA)\n",
    "y_pred = knn_clfA.predict(X_test)\n",
    "psA = precision_score(y_test, y_pred, average=None)\n",
    "print('\\npsA=', psA, np.average(psA))      \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
